
NUM_OF_CPU=$(nproc)

CLANG_HOST_TOOLCHAIN := $(OHOS_HOME)/prebuilts/clang/ohos/linux-x86_64/llvm/bin
KERNEL_HOSTCC := $(CLANG_HOST_TOOLCHAIN)/clang
KERNEL_PREBUILT_MAKE := make
CLANG_CC := $(CLANG_HOST_TOOLCHAIN)/clang

PATH := $(CLANG_HOST_TOOLCHAIN):$(PATH)
KERNEL_CROSS_COMPILE :=
KERNEL_ARCH := riscv
KERNEL_TARGET_TOOLCHAIN_PREFIX :=
KERNEL_CROSS_COMPILE += LLVM=1
KERNEL_CROSS_COMPILE += LLVM_IAS=1

# if KERNEL_TARGET_TOOLCHAIN is not defined, use the clang toolchain
ifeq ($(KERNEL_TARGET_TOOLCHAIN),)
	KERNEL_CROSS_COMPILE += CC="$(CLANG_CC)"
endif

KERNEL_CROSS_COMPILE += CROSS_COMPILE="$(KERNEL_TARGET_TOOLCHAIN_PREFIX)"

KERNEL_MAKE := \
    PATH="$(BOOT_IMAGE_PATH):$$PATH" \
    $(KERNEL_PREBUILT_MAKE)


DEVICE_PATCH_DIR := $(OHOS_HOME)/kernel/linux/patches/${KERNEL_VERSION}/$(DEVICE_NAME)_patch
DEVICE_PATCH_FILE := $(DEVICE_PATCH_DIR)/$(DEVICE_NAME).patch
KERNEL_IMAGE_OUT_FILE := $(LINUX_KERNEL_IMAGE_FILE)
DEFCONFIG_FILE := $(CONFIG_FILE_NAME)

export KBUILD_OUTPUT=$(LINUX_KERNEL_OBJ_OUT)

# build kernel
$(KERNEL_IMAGE_OUT_FILE):
	$(hide) echo "build kernel..."

	# copy kernel source to build path
	$(hide) rm -rf $(LINUX_KERNEL_SRC_OUT); mkdir -p $(LINUX_KERNEL_SRC_OUT); cp -arfL $(KERNEL_SOURCE_PATH)/* $(LINUX_KERNEL_SRC_OUT)/ || true

	$(hide) $(KERNEL_MAKE) -C $(LINUX_KERNEL_SRC_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) distclean
	$(hide) $(KERNEL_MAKE) -C $(LINUX_KERNEL_SRC_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) $(DEFCONFIG_FILE)
ifeq ($(KERNEL_VERSION), linux-5.10)
	$(hide) $(KERNEL_MAKE) -C $(LINUX_KERNEL_SRC_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) modules_prepare
endif
	$(hide) $(KERNEL_MAKE) -C $(LINUX_KERNEL_SRC_OUT) ARCH=$(KERNEL_ARCH) $(KERNEL_CROSS_COMPILE) -j$(NUM_OF_CPU) $(KERNEL_IMAGE)

$(info build kernel done)

.PHONY: build-kernel
build-kernel:
